import random
import sys

import pygame

from boids_phind import Boid

SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768

FPS = 60


class GameWorld:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.clock = pygame.time.Clock()

        self.boids = []
        for i in range(10):
            self.boids.append(Boid(random.randint(0, SCREEN_WIDTH),
                                   random.randint(0, SCREEN_HEIGHT)))

    def run(self):
        is_running = True
        while is_running:
            # events
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    is_running = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        is_running = False

            # clock
            dt = self.clock.tick(FPS)

            # update
            for b in self.boids:
                b.update(self.boids)

            # draw
            self.screen.fill((0, 40, 120))

            for b in self.boids:
                b.draw(self.screen)

            pygame.display.flip()

    def exit(self):
        pygame.quit()
        sys.exit()


def main():
    gw = GameWorld()
    gw.run()
    gw.exit()


if __name__ == '__main__':
    main()
